package com.id.supaclean.utils

import android.telephony.TelephonyManager
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.id.supaclean.MyApplication.Companion.appContext
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig

object Country {
    //国家
    private val DefaultCountry = arrayOf("jp", "tw", "hk", "kr", "us")

    fun manualConfig(vararg country: String): Boolean = therein(*country)

    fun remoteConfig(vararg defaultCountryConfig: String = DefaultCountry): Boolean {
        val country = Firebase.remoteConfig.getString("country")
            .let { if (it.isEmpty()) defaultCountryConfig else it.formatJson() }
        return manualConfig(*country)
    }

    private fun therein(vararg country: String): Boolean {
        val telephonyManager =
            appContext.getSystemService(AppCompatActivity.TELEPHONY_SERVICE) as TelephonyManager
        val countryIso = telephonyManager.networkCountryIso
        if (countryIso.isNullOrEmpty()) return false
        for (item in country) {
            if (countryIso.contentEquals(item, true)) return true
        }
        return false
    }
}